"""
0、数据清洗
1、读取excel数据
2、将原有的日期分来,分割出日期和星期两列
3、计算出温度差
4、温度等级规划
"""
import pandas as pd
filename = r"D:\Workspaces\study\Python\SimpleCode-Python\北京10年天气数据 (1).xlsx"
df=pd.read_excel(filename,engine="openpyxl")

def split_date(row):
    date,week = row["日期"].split()
    return date,week
df[["日期","星期"]] = df.apply(
    split_date,axis=1,result_type="expand"
)

df["最高温"] = df["最高温"].str.replace("°", "")
df["最高温"] = pd.to_numeric(df['最高温'], errors='raise')
df["最低温"] = df["最低温"].str.replace("°", "")
df['最低温'] = pd.to_numeric(df['最低温'], errors='raise')
df["温度差"] = df["最高温"] - df["最低温"]

def leave_wendu(wendu):
    try:
        wendu = float(wendu)
        if 35 <= wendu < 40:
            return '注意防暑'
        elif 30 <= wendu < 35:
            return '高温预警'
        elif 20 <= wendu < 30:
            return '天气暖和'
        elif 10 <= wendu < 20:
            return '天气凉爽'
        elif 0 <= wendu < 10:
            return '低温警告'
        elif wendu <= 0:
            return '防寒保暖'
        else:
            return '温度异常'
    except ValueError:
        return '输入错误，请输入一个有效的温度值'
    
df["温度等级"] = df["最高温"].apply(leave_wendu)
df.to_excel(filename, index=False, engine='openpyxl')
print(df.head())